package com.example.allshengmedical.experimentrun.activity;

import java.io.File;
import java.io.IOException;

import com.example.allshengmedical.util.FileManagerUtil;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

public class CommonSQLiteHelper {
	public static final String DATABASE_NAME = "myDatabase.db"; // 数据库名称
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase database;
	final static String AmplifucationTableName = "AmplifucationDB";
	final static String RongJieTableName = "RongJieDB";
	final static String TableName = "TempDB";
	public static final int DATABASE_VERSION = 1;
	String AmplifucationDB = "CREATE  TABLE  AmplifucationDB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	String TempDB = "CREATE  TABLE  TempDB ( "
			+ "_id Integer primary key , temp TEXT , temp2 TEXT , temp3 TEXT)";
	String RongJieDB = "CREATE  TABLE  RongJieDB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	String RongJiechannel1DB = "CREATE  TABLE  RongJiechannel1DB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	String RongJiechannel2DB = "CREATE  TABLE  RongJiechannel2DB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	String RongJiechannel3DB = "CREATE  TABLE  RongJiechannel3DB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	String RongJiechannel4DB = "CREATE  TABLE  RongJiechannel4DB ( "
			+ "_id Integer primary key , ChannelIndex Integer , LightNum Float , Line Integer , PointIndex Integer )";
	File file;

	public CommonSQLiteHelper(Context ctx) {
		this.context = ctx;

	}

	public SQLiteDatabase getSQL() {
		File file = createDBFile();
		SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(file,
				null);
		return database;
	}

	public void open() {
		File file = createDBFile();
		database = SQLiteDatabase.openOrCreateDatabase(file, null);
		this.DBHelper = new DatabaseHelper(this.context);
		DBHelper.onCreate(database);
	}

	public void deleteDBFile() {
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + DATABASE_NAME);
		if (file.exists()) {
			file.delete();
		}
	}

	public File createDBFile() {
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		fileManagerUtil.createDB();
		File file = new File(Environment.getExternalStorageDirectory() + "/"
				+ "AllShengDB" + "/" + DATABASE_NAME);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;

	}

	public void delete() {
		try {
			deleteDBFile();
			// file = createDBFile();
			// database = SQLiteDatabase.openOrCreateDatabase(file, null);
			// // onCreate(database);
			// if (database != null) {
			// database.execSQL("DELETE FROM " + AmplifucationTableName);
			// }
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			file = createDBFile();
			database = SQLiteDatabase.openOrCreateDatabase(file, null);
			database.execSQL(AmplifucationDB);
			database.execSQL(TempDB);
			database.execSQL(RongJieDB);
			// database.execSQL(RongJiechannel1DB);
			// database.execSQL(RongJiechannel2DB);
			// database.execSQL(RongJiechannel3DB);
			// database.execSQL(RongJiechannel4DB);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}

	}

	public void close() {
		this.DBHelper.close();
	}
}
